package com.nulldev.util.string;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

import com.nulldev.util.other.ArrayUtils;

public class AggregateTranslator extends CharSequenceTranslator {
    private final CharSequenceTranslator[] translators;
    
    public AggregateTranslator(final CharSequenceTranslator... translators) {
        this.translators = ArrayUtils.clone(translators);
    }
    
    @Override
    public int translate(final CharSequence input, final int index, final Writer out) throws IOException {
        for (final CharSequenceTranslator translator : this.translators) {
            final int consumed = translator.translate(input, index, out);
            if (consumed != 0) {
                return consumed;
            }
        }
        return 0;
    }
    
    public String translate(final String input) throws IOException {
    	final StringWriter sw = new StringWriter();
    	final int _q = this.translate(input, 0, sw);
    	sw.flush();
    	final String swo = sw.toString();
    	sw.close();
    	return swo;
    }
}

